// myself/file/QiyeIssueList.js
import api from "../../utils/api.js";
import utils from "../../utils/utils.js";
import network from "../../utils/network.js";
import formatDate from "../../utils/format_date.js";
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: {
      list: [{
        id: 0,
        title: "待我审批",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 1,
        title: "我的申请",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      },{
        id: 2,
        title: "经我处理",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }],
      selectedId: 0,
      scroll: false
    },
    activityIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onPullDownRefresh();

    this.toast = this.selectComponent("#toast");
    // notification.addNotification("RefreshOrder", this.refreshOrder, this);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // notification.removeNotification("RefreshOrder", this);
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.setData({
      [`tab.list[${this.data.activityIndex}].page`]: 1,
      [`tab.list[${this.data.activityIndex}].nowPage`]: 0,
      [`tab.list[${this.data.activityIndex}].maxPage`]: 1,
      [`tab.list[${this.data.activityIndex}].details`]: []
    })
    this.request();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.request();
  },

  /**
   * 请求数据
   * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
   */
  request: function () {
    let item = this.data.tab.list[this.data.activityIndex];
    let param = {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      loadci: 1,
      storebeforeaction: 'flowbillbefore',
      storeafteraction: 'flowbillafter',
      modeid: 117
    }
    if (item.id == 0) {
      param.atype = 'daib';
      param.page = item.page
    } else if(item.id == 1) {
      param.atype = 'my';
      param.page = item.page
    }else{
      param.atype = 'jmy';
      param.page = item.page
    }

    if (item.nowPage == item.maxPage) {
      this.toast.showToast("暂无更多");
    } else {
      network.postJson(this, api.fwork, param).then(res => {
        this.setData({
          [`tab.list[${this.data.activityIndex}].details[${item.nowPage}]`]: res.data.data.rows,
          [`tab.list[${this.data.activityIndex}].page`]: res.data.data.nextpage
        })

        this.data.tab.list[this.data.activityIndex].nowPage = res.data.data.page;
        this.data.tab.list[this.data.activityIndex].maxPage = res.data.data.maxpage;
      });
    }
  },

  /**
   * 点击tab
   */
  tabTapChange: function (e) {
    this.setData({
      activityIndex: e.detail.index,
      'tab.selectedId': e.detail.selectedId
    })
    this.onPullDownRefresh();
  },

  refreshOrder: function () {
    this.onPullDownRefresh();
  },

  goDocumentSigning(e) {
    let mid = e.currentTarget.dataset.mid;
    let modelname = e.currentTarget.dataset.modelname;
    wx.navigateTo({
      url: '/myself/file/DocumentSigning?mid=' + mid + '&modelname=' + modelname
    })
  }
})